package cn.didi.feidi.androidorder.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.model.BalanceDetailItem;

/**
 * Created by 独步清风 on 2017/7/25.
 */

public class PersonBalanceDetailListViewAdapter extends ArrayAdapter<BalanceDetailItem> {

    private List<BalanceDetailItem> mArrayList;

    private int resourceId;

    public PersonBalanceDetailListViewAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<BalanceDetailItem> objects) {
        super(context, resource, objects);
        mArrayList = objects;
        resourceId = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        BalanceDetailItem balanceDetail = getItem(position); // 获取当前项的BalanceDetailItem实例

        View view;
        ViewHolder viewHolder;
        Log.d("test view","start");
        if (convertView == null) {
            Log.d("test view","convertView == null");
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.businessSource = (TextView) view.findViewById(R.id.balance_detail_item_source);
            viewHolder.businessTime = (TextView) view.findViewById(R.id.balance_detail_item_time);
            viewHolder.businessValues  = (TextView) view.findViewById(R.id.balance_detail_item_values);
            viewHolder.walletBalance = (TextView) view.findViewById(R.id.balance_detail_item_wallet_balance);
            view.setTag(viewHolder); // 将ViewHolder存储在View中
        } else {
            Log.d("test view","convertView != null");
            view = convertView;
            viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder
        }
        viewHolder.businessSource.setText(balanceDetail.getBusinessSource());
        viewHolder.businessTime.setText(balanceDetail.getBusinessTime());
        viewHolder.businessValues.setText(balanceDetail.getBusinessValues());
        viewHolder.walletBalance.setText(balanceDetail.getWalletBalance());
        return view;
    }

    class ViewHolder{
        TextView businessSource;
        TextView businessTime;
        TextView businessValues;
        TextView walletBalance;
    }
}
